<?php

namespace plugins\user_other\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Model;
use plugins\user_other\model\PluginUserOtherModel;


class AdminIndexController extends PluginAdminBaseController
{
    public $admin_id;
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 用户列表
     * @adminMenu(
     *     'name'   => '用户列表',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000, 
     *     'icon'   => '',
     *     'remark' => '通知列表',
     *     'param'  => ''
     * )
     */
    public function user_other_index()
    {
        $data = $this->request->param();
        $where = [];
		$where['user_type'] = ['eq', 2];// 设置只读取会员
        if (!empty($data['uid'])) {
            $where['id'] = ['eq', intval($data['uid'])];
        }
		$keywordComplex = [];
        if (!empty($data['keyword'])) {
            $keyword = $data['keyword'];
            $keywordComplex['user_login|user_nickname|user_email|mobile']    = ['like', "%$keyword%"];
        }
        $list = Db::name('user')->whereOr($keywordComplex)->where($where)->order('id', 'desc')->paginate(20);
        $page = $list->render();
        $list =  PluginUserOtherModel::get_role($list->items());
        $this->assign('rank', PluginUserOtherModel::get_rank());
        $this->assign('users', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 新增用户
     * @adminMenu(
     *     'name'   => '新增用户',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000, 
     *     'icon'   => '',
     *     'remark' => '新增用户',
     *     'param'  => ''
     * )
     */
    public function user_other_add()
    {
        $data = $this->request->param();
        if (!empty($data['id'])) {
            $user = Db::name('user')->where('id', $data['id'])->find();
            $this->assign('user', $user);
        }
        $this->assign('user_status', PluginUserOtherModel::user_status());
		
		// 会员等级列表
		$ranklist =  Db::name('user_rank')->column('id,name');
		$this->assign('ranklist', $ranklist);
		
        return $this->fetch();
    }
	
	/**
     * 会员等级
     * @adminMenu(
     *     'name'   => '会员等级',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000, 
     *     'icon'   => '',
     *     'remark' => '会员等级',
     *     'param'  => ''
     * )
     */
    public function user_other_rank()
    {
        $data = Db::name('user_rank')->order(["list_order" => "DESC", "id" => "ASC"])->select();
        $this->assign("ranklist", $data);
        return $this->fetch();
    }
	
	/**
     * 会员等级添加
     * @adminMenu(
     *     'name'   => '会员等级添加',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000, 
     *     'icon'   => '',
     *     'remark' => '会员等级添加',
     *     'param'  => ''
     * )
     */
    public function user_other_rank_add()
    {
        return $this->fetch();
    }
	
	/**
     * 添加等级提交
     * @adminMenu(
     *     'name'   => '添加等级提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加等级提交',
     *     'param'  => ''
     * )
     */
    public function user_other_rank_add_post()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'Rank');
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
				$resdata = array();
				$resdata['list_order'] = $data['list_order'];
				$resdata['name'] = $data['name'];
				$resdata['upgradeif'] = $data['upgradeif'];
				$resdata['discount'] = $data['discount'];
				$resdata['remark'] = $data['remark'];
				$resdata['status'] = $data['status'];
                $result = Db::name('user_rank')->insert($resdata);
                if ($result) {
                    $this->success("添加等级成功", cmf_plugin_url('UserOther://admin_index/user_other_rank'));
                } else {
                    $this->error("添加等级失败");
                }

            }
        }
    }

    /**
     * 编辑等级
     * @adminMenu(
     *     'name'   => '编辑等级',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑等级',
     *     'param'  => ''
     * )
     */
    public function user_other_rank_edit()
    {
		$id = $this->request->param("id", 0, 'intval');
        $data = Db::name('user_rank')->where(["id" => $id])->find();
        if (!$data) {
            $this->error("该等级不存在！");
        }
        $this->assign("data", $data);
        return $this->fetch();
    }

    /**
     * 编辑等级提交
     * @adminMenu(
     *     'name'   => '编辑等级提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑等级提交',
     *     'param'  => ''
     * )
     */
    public function user_other_rank_edit_post()
    {
        $id = $this->request->param("id", 0, 'intval');
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'Rank');
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);

            } else {
				$resdata = array();
				$resdata['id'] = $id;
				$resdata['list_order'] = $data['list_order'];
				$resdata['name'] = $data['name'];
				$resdata['upgradeif'] = $data['upgradeif'];
				$resdata['discount'] = $data['discount'];
				$resdata['remark'] = $data['remark'];
				$resdata['status'] = $data['status'];
                if (Db::name('user_rank')->update($resdata) !== false) {
                    $this->success("保存成功！", cmf_plugin_url('UserOther://admin_index/user_other_rank'));
                } else {
                    $this->error("保存失败！");
                }
            }
        }
    }

    /**
     * 删除等级
     * @adminMenu(
     *     'name'   => '删除等级',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除等级',
     *     'param'  => ''
     * )
     */
    public function user_other_rank_delete()
    {
        $id = $this->request->param("id", 0, 'intval');
        $count = Db::name('user')->where(['rank_id' => $id])->count();
        if ($count > 0) {
            $this->error("该等级已经有用户！");
        } else {
            $status = Db::name('user_rank')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", cmf_plugin_url('UserOther://admin_index/user_other_rank'));
            } else {
                $this->error("删除失败！");
            }
        }
    }

    /**
     * @msg: 传入
     */
    public  function user_other_post($user = false)
    {
        $data = $this->request->param();

        $need = PluginUserOtherModel::need_in();
        foreach ($need as $n => $ndd) {
            if (empty($data[$n])) {
                $this->Error($ndd . '不能为空！');
            }
        }

        if (!empty($data['id'])) {
            $user = Db::name('user')->where('id', $data['id'])->find();
            if (empty($user)) {
                $this->Error('传入错误！');
            }
        }

        if (empty($user)) {
            $user = PluginUserOtherModel::user_data();
        }

        $only = PluginUserOtherModel::user_only($data);
        if ($only) {
            $this->Error($only);
        }

        if (PluginUserOtherModel::update_user($data, $user)) {
            $this->Success('操作成功！', cmf_plugin_url('UserOther://admin_index/user_other_index'));
        }
        $this->Error('操作失败！');
    }

    /**
     * @msg: 删除用户
     */
    public  function user_other_delete()
    {
        $data = $this->request->param();
        if (!empty($data['id'])) {
            if (Db::name('user')->where('id', $data['id'])->delete()) {
                $this->Success('操作成功！', cmf_plugin_url('UserOther://admin_index/user_other_index'));
            }
        }
        $this->Error('操作失败！');
    }

    /**
     * @msg: 拉黑用户
     */
    public  function user_other_ban()
    {
        $data = $this->request->param();
        if (!empty($data['id'])) {
            $user = Db::name('user')->where('id', $data['id'])->find();
            if ($user) {
                if ($user['user_status'] == 1) {
                    $user['user_status']  = 0;
                } else {
                    $user['user_status']  = 1;
                }
                if (Db::name('user')->update($user)) {
                    $this->Success('操作成功！', cmf_plugin_url('UserOther://admin_index/user_other_index'));
                }
            }
        }
        $this->Error('操作失败！');
    }
}
